﻿

function addRecord()
{
    var table = $("#tblRecords");
    var newRow = table.find("#sampleRow").clone();
    newRow.removeAttr("id").css("display", "table-row");
    newRow.find("select[name=ddlRecord]").removeAttr("disabled");
    newRow.find("input[name=hdfRecordId]").removeAttr("disabled");
    newRow.find("input[name=tbxRecordValue]").removeAttr("disabled");
    newRow.find("input[name=tbxRecordStartTime]").removeAttr("disabled");
    newRow.find("input[name=tbxRecordEndTime]").removeAttr("disabled");
    newRow.find("input[name=tbxRecordRating]").removeAttr("disabled");
    
    var tbxId = "tbxRecordStartTime" + randomString();
    newRow.find("input[name=tbxRecordStartTime]").attr("id", tbxId);
    
    var tbxId2 = "tbxRecordEndTime" + randomString();
    newRow.find("input[name=tbxRecordEndTime]").attr("id", tbxId2);
    
    table.append(newRow);
    
    new JsDatePick({
        useMode:2,
        target:tbxId,
        dateFormat:"%d/%m/%Y"
    });
    new JsDatePick({
        useMode:2,
        target:tbxId2,
        dateFormat:"%d/%m/%Y"
    });
}

function removeRecord(button)
{
    var row = $(button.parentNode.parentNode);
    var recordId = row.find("input[name=hdfRecordId]").val();
    if (recordId != "")
    {
        var div = $("#divRemovedRecords");
        var newId = div.find("#sampleRemovedRecordId").clone().removeAttr("id").removeAttr("disabled");
        newId.val(recordId);
        div.append(newId);
    }
    row.remove();
}

function selectAssets()
{
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Asset/AssetSelection.aspx",
        argsVariable, 
        "dialogWidth:800px; dialogHeight:400px; dialogLeft:250px; dialogTop:200px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
    
    if (answer != "cancel")
    {
        var table = $("#tblAssets");
        for (var i=0; i<answer.selectedAssets.length; i++)
        {
            var existedIds = $("input[name=hdfAssetId]");
            var already = false;
            for (var j=0; j<existedIds.length; j++)
            {
                if (existedIds.eq(j).val() == answer.selectedAssets[i].assetId)
                {
                    already = true;
                    break;
                }
            }
        
            if (!already)
            {
                var newRow = $("#sampleAsset").clone().removeAttr("id").css("display", "table-row");
                newRow.find("input[name=hdfAssetId]").val(answer.selectedAssets[i].assetId).removeAttr("disabled");
                newRow.find("input[name=tbxAssetName]").val(answer.selectedAssets[i].assetName);
                newRow.find("input[name=tbxAssetDescription]").removeAttr("disabled");
                var tbxId = "tbxAssetStartDate" + randomString();
                newRow.find("input[name=tbxAssetStartDate]").removeAttr("disabled").attr("id", tbxId);
                var tbxId2 = "tbxAssetStartDate" + randomString();
                newRow.find("input[name=tbxAssetEndDate]").removeAttr("disabled").attr("id", tbxId2);
                
                table.append(newRow);
                new JsDatePick({
                useMode:2,
                target:tbxId,
                dateFormat:"%d/%m/%Y"
                });
                new JsDatePick({
                    useMode:2,
                    target:tbxId2,
                    dateFormat:"%d/%m/%Y"
                });
                
                var removedIds = $("input[name=hdfRemovedAssetId]");
                for (var k=0; k<removedIds.length; k++)
                {
                    if (removedIds.eq(k).val() == answer.selectedAssets[i].assetId)
                    {
                        removedIds.eq(k).remove();
                        break;
                    }
                }
            }
        }
    }
}

function removeAsset(button)
{
    var row = $(button.parentNode.parentNode);
    var div = $("#divRemovedAssets");
    var newId = div.find("#sampleRemovedAssetId").clone().removeAttr("id").removeAttr("disabled");
    newId.val(row.find("input[name=hdfAssetId]").val());
    div.append(newId);
    row.remove();
}